package uscdb.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import uscdb.pageModel.DataGrid;
import uscdb.pageModel.Json;
import uscdb.pageModel.PageHelper;
import uscdb.pageModel.Product;
import uscdb.service.ProductServiceI;

/**
 * 产品控制器
 * 
 * @author SYSTEM  
 *
 */
@Controller
@RequestMapping("/productController")
public class ProductController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ProductController.class);

	@Autowired
	private ProductServiceI productService;

	/**
	 * 添加 产品
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Product product) {
		Json json = new Json();
		try {
			productService.add(product);
			json.setSuccess(true);
			json.setMsg("添加成功！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setMsg("系统故障，请联系管理员");
		}
		return json;
	}

	/**
	 * 修改 产品
	 * 
	 * @param product
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Product product) {
		Json json = new Json();
		try {
			productService.edit(product);
			json.setSuccess(true);
			json.setMsg("修改成功！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setMsg("系统故障，请联系管理员");
		}
		return json;
	}

	/**
	 * 删除 产品
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String ids) {
		Json json = new Json();
		try {
			json = productService.delete(ids);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setMsg("系统故障，请联系管理员");
		}
		return json;
	}

	/**
	 * 获取 产品 数据表格
	 * 
	 * @param product,PageHelper
	 * @return
	 */
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(Product product, PageHelper ph) {
		return productService.dataGrid(product, ph);
	}

	/**
	 * 跳转到 product 管理页面
	 * 
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		return "/admin/product";
	}

	/**
	 * 功能：导入logic时必须选择产品
	 * 
	 * @param q
	 *            参数
	 * @return
	 */
	@RequestMapping("/productCombobox")
	@ResponseBody
	public List<Product> productCombobox(String q) {
		List<Product> pList = productService.productCombobox(q);
		return pList;
	}

}
